package gits

import (
	"fmt"
	"github.com/go-git/go-git/v5"
)

// DownloadGit 云端下载 Git 仓库
func DownloadGit(directory string, url string, branch string) (*git.Repository, error) {
	return Clone2Disk(directory, url, branch, true)
}

// UploadGit 数据上传
func UploadGit(directory string, url string, branch string, files []string) error {
	r, err := Clone2Disk(directory, url, branch, true)
	if err != nil {
		println(fmt.Sprintf("GitCloneError|%s", err.Error()))
		return err
	}

	// ADD (循环添加)
	for i := 0; i < len(files); i++ {
		cacheFile := files[i]
		hash, addError := Add(r, cacheFile)
		if err != nil {
			println(fmt.Sprintf("GitAddError|%s", addError.Error()))
			return err
		}
		fmt.Println(hash.String())
	}

	// Commit
	commit, commitError := Commit(r, "change")
	if err != nil {
		println(fmt.Sprintf("GitCommitError|%s", commitError.Error()))
		return err
	}
	commitId := commit.ID().String()
	fmt.Println(commitId)

	// PUSH
	pushError := Push(r)
	if err != nil {
		println(fmt.Sprintf("GitCommitError|%s", pushError.Error()))
		return err
	}
	fmt.Println("Push Done")
	return nil
}
